//
//  Map.m
//  lyxluyou
//
//  Created by mmyy on 15/10/17.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "Map.h"

Map *map = nil;
@implementation Map
{
    CLLocationManager *_locationManager;

}
+(instancetype)shareMap
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (map == nil) {
            map = [[self alloc] init];
            map.coor = CLLocationCoordinate2DMake(40.04809600, 116.328);
        }
    });
    return map;
}
-(void)startLocation
{
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    //判断当前版本号
    if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 8.0) {
        [_locationManager requestWhenInUseAuthorization];//调用授权方法
    }
    [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];//设置定位精度
    
    [_locationManager setDistanceFilter:10];//设置位置跟最新回调的最小范围
    
    [_locationManager startUpdatingLocation];
    

}
-(void)searchMapWithString:(NSString *)string success:(MapSuccess)success andFail:(MapFailure)fail
{
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:string completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error) {
            fail(error);
        }else {
            success(placemarks);
        }
    }];

}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *currentLocation = [locations firstObject];
    map.coor = currentLocation.coordinate;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CoorSuccess" object:nil userInfo:nil];
}

-(void)reverseGeocodeLocation:(CLLocation *)location success:(MapSuccess)success andFail:(MapFailure)fail
{
    CLGeocoder *gecoder = [[CLGeocoder alloc] init];
    [gecoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error) {
            fail(error);
        }else {
            success(placemarks);
        }
    }];
}



@end
